<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 商品规格操作功能
 * 
 * @package 	htdocs
 * @subpackage  quanliadmin
 * @author  	create by allen.qiang
 */
quanli_require('module/base/goods/goodsSpec.class.php');
class pa_goodsSpec {

    public function execute() {

		global $G_PGLOB ;

        if (isset($_REQUEST['operFlg'])) {

            switch($_REQUEST['operFlg']) {

                case 1: //商品规格列表
                    $this->specList();
                    break;
                case 2: //商品规格添加
                    $this->specAdd();
                    break;
                case 3: //商品规格编辑
                    $this->specEdit();
                    break;
				default:
					$G_PGLOB->smarty->assign('error_msg', '参数错误，请审核后再操作');
					$G_PGLOB->smarty->display('comm/error.tpl');
            }
        }
    }

    /**
     * 商品规格列表
     * @author sunchun
     * 
     */
    private function specList() {
    	
        global $G_SHOP;
		
		//规格编辑权限
		$G_SHOP->smarty->assign('edit_permission', $G_SHOP->has_permission(512));

        $G_SHOP->smarty->display('goods/goodsSpecList.tpl');
    }
    
    /**
     * 规格添加 
     *
     */
    private function specAdd() {
    	global $G_PGLOB;
    	
    	//得到最大的规格排序
    	$maxSortOrder = $G_PGLOB->DBCA->stmt_query('SELECT IF(MAX(sortOrder) IS NULL ,1 , MAX(sortOrder)+1) AS max_sortOrder FROM goods_spec') ;
		
    	$arg_array = array( 'sortname'  => 'specGroupID', 
					        'sortorder' => '', 
					        'page' 		=> 0, 
					        'rp' 		=> 0) ;
					        
     	$commHandler = new commHandler( $G_PGLOB->DBCA ) ;
		$result_data = $commHandler->search($arg_array, 'goods_spec_group' ,'' ) ;
    	
    	//统一传参数组
    	$specInfo = new entity('goods_spec') ;
    	$specInfo->sortOrder = $maxSortOrder[0]->max_sortOrder ;
    	$specInfo->useFlg    = 1 ;
    	$specInfo->specGroup = $result_data ;
    	
    	$G_PGLOB->smarty->assign('specInfo' , $specInfo);
    	$G_PGLOB->smarty->display('goods/spec_edit.tpl');
    }
    
    /**
     * 规格编辑 
     *
     */
    private function specEdit() {
    	global $G_PGLOB;
    	
    	//得到规格信息
    	$specID   = $_REQUEST['specID'] ;
    	
    	
    	$arg_array = array( 'sortname'  => 'specGroupID', 
					        'sortorder' => '', 
					        'page' 		=> 0, 
					        'rp' 		=> 0) ;
					        
     	$commHandler = new commHandler( $G_PGLOB->DBCA ) ;
		$result_data = $commHandler->search($arg_array, 'goods_spec_group' ,'' ) ;
    	
    	$specInfo = new entity('goods_spec') ;
    	$specInfo->specID = $specID ;
    	if(!$G_PGLOB->DBCA->fetch( $specInfo )){
    		$G_PGLOB->smarty->assign('error_msg', sprintf('规格号「%d」,的规格不存在，请审核后再操作' , $specID));
			$G_PGLOB->smarty->display('comm/error.tpl');
			die() ;
    	}
    	$specInfo->specGroup = $result_data ;
    	
       	$G_PGLOB->smarty->assign('specInfo' , $specInfo);
    	$G_PGLOB->smarty->display('goods/spec_edit.tpl');
    } 
    
}